package me.jessyan.mvparms.demo.mvp.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v7.content.res.AppCompatResources;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.xrecyclerview.XRecyclerView;
import com.example.xrecyclerview.refresh.SwipyRefreshLayout;
import com.example.xrecyclerview.refresh.SwipyRefreshLayoutDirection;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.mvparms.demo.R;

/**
 * @author DrChen
 * @Date 2019/2/26 0026.
 * qq:1414355045
 *  我的优惠券
 */
public class MyCouponVpAdapter extends PagerAdapter{
    private int layoutId;
    private List<String> list = new ArrayList<>();
    private LayoutInflater inflater;
    private List<Boolean> booleanList;
    private Context context;

    private MyCouponVpAdapterListener<Boolean> listener;

    public void setListener(MyCouponVpAdapterListener<Boolean> listener) {
        this.listener = listener;
    }

    public MyCouponVpAdapter(int layoutId, List<String> list, Context context) {
        this.layoutId = layoutId;
        this.list = list;
       this. context = context;
        inflater = LayoutInflater.from(context);

       booleanList = new ArrayList<>();
        booleanList.add(true);
        booleanList.add(false);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(false);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
        booleanList.add(true);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view ==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = inflater.inflate(layoutId,null);
          bindView(view,booleanList,position);
        container.addView(view,0);
        return  view;
    }

    /**
     *
     *  列表视图
     */
    /**
     *
     * @param view
     * @param t
     * @param type positon 表示哪个列表
     */
    private void bindView(View view, List<Boolean> t, int type) {
        SwipyRefreshLayout refreshLayout = view.findViewById(R.id.refresh);
        XRecyclerView recyclerView = view.findViewById(R.id.recyclerView);


        //刷新，发起网络请求
        refreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {

            }
        });



        recyclerView.setPullRefreshEnabled(false);
        recyclerView.clearHeader();

        LinearLayoutManager lr = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
        recyclerView .setLayoutManager(lr);
        ArmsUtils.configRecyclerView(recyclerView,lr);
        DefaultAdapter adapter = new DefaultAdapter(t,true) {
            @Override
            public BaseHolder getHolder(View v, int viewType) {
                return new BaseHolder<Boolean>(v) {
                    @Override
                    public void setData(Boolean data, int pos) {
                        ImageView labelIv = v.findViewById(R.id.label_iv);
                        Drawable drawable;
                        //注意这type 是表示哪个列表
                        switch (type){
                            case 0:
                                labelIv.setVisibility(View.VISIBLE);
                                if(data){
                               drawable = AppCompatResources.getDrawable(v.getContext(),R.mipmap.colse_expiration_label);
                                }else {
                                    drawable = AppCompatResources.getDrawable(v.getContext(),R.mipmap.new_label);
                                }
                                drawable .setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                                labelIv.setImageDrawable(drawable);
                                break;
                            case 1:
                                labelIv.setVisibility(View.VISIBLE);
                              drawable  = AppCompatResources.getDrawable(v.getContext(),R.mipmap.used_label);
                                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                                labelIv.setImageDrawable(drawable);
                                break;
                            case 2:
                                labelIv.setVisibility(View.VISIBLE);
                                drawable  = AppCompatResources.getDrawable(v.getContext(),R.mipmap.overdue_label);
                                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                                labelIv.setImageDrawable(drawable);
                                break;
                                default:labelIv.setVisibility(View.GONE);
                        }
                    }
                };
            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_my_coupon;
            }


        };
        recyclerView.setAdapter(adapter);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() { }

            @Override
            public void onLoadMore() {

            }
        });

adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener<Boolean>() {
    @Override
    public void onItemClick(View view, int viewType, Boolean data, int pos) {
        if(DoubleClickUtils.getInstance().isDouble(view)){
            return;
        }
        if(listener!=null){
            listener.getItem(view,type,data);
        }


    }
});

    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
    }

    public  interface MyCouponVpAdapterListener<T> {
        void getItem(View view, int type,T data);
    }


}
